package com.block.product.service.union;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import com.block.product.utils.XinYeBangUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class XinYeBangProductServiceImpl implements IUnionProductService {

    private static Logger log = LogFactory.getCommLog();

//    private static final String REQUEST_URL = "https://qyh.qyihua.com/api/lianDeng";
//    private static final String REQUEST_URL = "https://testqyh.qyihua.com/api/lianDeng";

//    private static final String REQUEST_URL = "http://testqyh.qyihua.com/api/lianDeng";

    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {
        try {

            String requestMatchUrl = productInfo.getChannelMatchUrl();

            HttpRequest httpRequest = HttpUtil.createPost(requestMatchUrl);
            httpRequest.header("Content-Type", "application/json");

            String md5Mobile = SecureUtil.md5(userInfoVo.getMobile()).toUpperCase();
            Long timestamp = System.currentTimeMillis();

            SortedMap<String,Object> bizMap = new TreeMap<>();
            bizMap.put("action", "hitLibrary");
            bizMap.put("timestamp", timestamp);
            bizMap.put("mobile", md5Mobile);

            String sign = StrUtil.format("{}&secretKey={}", XinYeBangUtils.getSign2(bizMap), productInfo.getChannelSecret());
            System.out.println(sign);
            bizMap.put("sign", SecureUtil.md5(sign).toUpperCase());

            String apisc = XinYeBangUtils.encrypt(productInfo.getChannelSecret(), JSON.toJSONString(bizMap));

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("apics", apisc);
            paramMap.put("appkey", productInfo.getChannelKey());
            paramMap.put("version", "2.0");

            httpRequest.body(JSON.toJSONString(paramMap));
            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【信业帮】撞库完成【resultStr】{}【REQUEST_URL】{}【bizMap】{}【paramMap】{}",
                    resultStr, requestMatchUrl, JSON.toJSONString(bizMap), JSON.toJSONString(paramMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            Integer code = jsonObject.getInteger("code");
            if (!ObjectUtil.equals(Integer.valueOf("0"), code)){
                return new ResponseVo(AppRspCodeConstant.THRID_FAILED);
            }

            return new ResponseVo(AppRspCodeConstant.SUCCESS);
        }catch (Exception exception){
            log.error("【产品联登】【信业帮】撞库异常【userInfoVo】" + JSON.toJSONString(userInfoVo), exception);
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
        }
    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {
        try {

            String requestRegisterUrl = productInfo.getChannelMatchUrl();

            HttpRequest httpRequest = HttpUtil.createPost(requestRegisterUrl);
            httpRequest.header("Content-Type", "application/json");

            Long timestamp = System.currentTimeMillis();

            SortedMap<String,Object> bizMap = new TreeMap<>();
            bizMap.put("action", "lianDeng");
            bizMap.put("timestamp", timestamp);
            bizMap.put("mobile", userInfoVo.getMobile());

            String sign = StrUtil.format("{}&secretKey={}", XinYeBangUtils.getSign2(bizMap), productInfo.getChannelSecret());
            System.out.println(sign);
            bizMap.put("sign", SecureUtil.md5(sign).toUpperCase());

            String apisc = XinYeBangUtils.encrypt(productInfo.getChannelSecret(), JSON.toJSONString(bizMap));

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("apics", apisc);
            paramMap.put("appkey", productInfo.getChannelKey());
            paramMap.put("version", "2.0");

            httpRequest.body(JSON.toJSONString(paramMap));
            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【信业帮】【注册】注册完成【result】{}【MATCH_USER_URL】{}【paramMap】{}",
                    resultStr, requestRegisterUrl, JSON.toJSONString(paramMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            Integer code = jsonObject.getInteger("code");
            Integer resultType = jsonObject.getInteger("resultType");
            String msg = jsonObject.getString("msg");
            if (ObjectUtil.equals(Integer.valueOf("0"), code) &&
                    ObjectUtil.equals(Integer.valueOf("100"), resultType)){

                String data = jsonObject.getString("resultUri");
                JSONObject resultData=  new JSONObject();
                resultData.put("productUrl", data);

                return new ResponseVo(AppRspCodeConstant.SUCCESS, resultData);
            }
            return new ResponseVo(AppRspCodeConstant.THRID_FAILED, msg, "");
        }catch (Exception exception){
            log.error("【产品联登】【今日必下】【注册】注册异常【userInfoVo】" + JSON.toJSONString(userInfoVo), exception);
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
        }
    }


    public static void main(String[] args) {
        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("action", "hitLibrary");
        bizMap.put("timestamp", "asdf");
        bizMap.put("mobile", "asdfaz");
        bizMap.put("sign", "zvasd");

        Object[] objects = bizMap.keySet().toArray();

        Arrays.sort(objects);

        System.out.println(JSON.toJSONString(objects));

    }
}
